<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>常见问题库管理系统v1.0</title>
</head>
<body>
<div id="wrap">
    <h1>添加常见问题</h1>
    <table>
        <tr>
            <th>分类：</th>
            <td>
                <select id="selClass">

                </select>
            </td>
        </tr>
        <tr>
            <th>标题：</th>
            <td>
                <input type="text" id="title">
            </td>
        </tr>
        <tr>
            <th>内容：</th>
            <td>
                <textarea id="content" rows="10" cols="40"></textarea>
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <input type="button" value="保存" id="save">
                <input type="button" value="放弃" id="cancel">
            </td>
        </tr>
    </table>
</div>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
<script>
    $(function () {
        loadAllClasses();

        $('#save').click(function () {
            saveFaq();
        });
        
        $('#cancel').click(function () {
            window.location.href = 'index.html';
        });
    });

    function saveFaq(){
        let classId = $('#selClass').val();
        let title = $('#title').val();
        let content = $('#content').val();

        if(classId == "0"){
            alert('请选择分类');
            $('#selClass').focus();
            return false;
        }
        else if($.trim(title) == ""){
            alert('请填写标题');
            $('#title').focus();
            return false;
        }
        else if($.trim(content) == ""){
            alert('请填写内容');
            $('#content').focus();
            return false;
        }else{
            let obj = {"title":title,"content":content,"classId":classId};
            let baseUrl = "http://localhost:8081/api/faq";
            $.ajax({
                type:'post',
                url:baseUrl + '/save',
                data:JSON.stringify(obj),
                dataType:'json',
                contentType:'application/json',
                success:function (result) {

                   if(result.code === 200){
                       alert(result.message);
                       window.location.href = 'index.html';
                   }
                   else{
                       alert(result.message);
                   }
                }
            });
        }
    }
    
    function loadAllClasses() {
        let baseUrl = 'http://localhost:8081/api/class';
        $.ajax({
            type:'get',
            url:baseUrl + '/list',
            data:{},
            dataType:'json',
            success:function (result) {
                let html = `<option value="0">--请选择分类--</option>`;
                $.each(result.data,function (index,clazz) {
                    html +=`<option value="${clazz.id}">${clazz.cname}</option>`;
                });
                $('#selClass').html(html);
            }
        });
    }
</script>
</body>
</html>